package com.syq.service.impl;

import com.syq.constant.CacheConstants;
import com.syq.constant.OperatorConstants;
import com.syq.feign.CacheFeign;
import com.syq.model.StandardSubmit;
import com.syq.service.FilterChainService;
import com.syq.util.CheckMobileOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Set;

/**
 * @ClassName: PhaseFilterServiceImpl
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/8/27 9:20
 * 实现号段补全策略*
 */

@Service("phase")
public class PhaseFilterServiceImpl implements FilterChainService {

    @Autowired
    private CacheFeign cacheFeign;

    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        System.out.println("执行号段补全策略");
        //补全区域信息
        String destMobile = standardSubmit.getDestMobile();
        String preMobile = destMobile.substring(0, 6);
        String key= CacheConstants.CACHE_PREFIX_PHASE+preMobile;
        String value = cacheFeign.get(key);
        if (StringUtils.isEmpty(value)){
            standardSubmit.setProvinceId(OperatorConstants.CHINA_UNKNOWN);
            standardSubmit.setCityId(OperatorConstants.CHINA_UNKNOWN);
        }else {
            String[] p = value.split(",");
            standardSubmit.setProvinceId(Integer.parseInt(p[0]));
            standardSubmit.setCityId(Integer.parseInt(p[1]));

        }
        //判断运营商
        if (CheckMobileOperator.isChinaMobilePhoneNum(destMobile)){
            standardSubmit.setOperatorId(OperatorConstants.CHINA_MOBILE);
        }else if (CheckMobileOperator.isChinaTelecomPhoneNum(destMobile)){
            standardSubmit.setOperatorId(OperatorConstants.CHINA_TELECOM);
        }else if (CheckMobileOperator.isChinaUnicomPhoneNum(destMobile)){
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNICOM);
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNICOM);
        }else {
            standardSubmit.setOperatorId(OperatorConstants.CHINA_MOBILE);
        }

        //网关ID
        String keys=CacheConstants.CACHE_PREFIX_SMS_CHANNEL+standardSubmit.getOperatorId()+":*";
        Set<String> setKeys = cacheFeign.keys(keys);
        for (String setKey : setKeys) {
            String[] split = setKey.split(":");
            standardSubmit.setGatewayID(Integer.parseInt(split[2]));
        }

        //下发源号码
        String spnumberKey=CacheConstants.CACHE_PREFIX_SMS_CHANNEL+standardSubmit.getOperatorId()+":"+standardSubmit.getGatewayID();
        String spnumber = cacheFeign.hget(spnumberKey, "spnumber");
        String extendNumberKey=CacheConstants.CACHE_PREFIX_ROUTER+standardSubmit.getClientID()+":"+standardSubmit.getGatewayID();
        Map map = cacheFeign.hMGet(extendNumberKey);
        String extendnumber = (String) map.get("extendnumber");
        standardSubmit.setSrcNumber(spnumber+extendnumber);
        //费用
        String price = (String) map.get("price");
        standardSubmit.setFee(Long.parseLong(price));
        System.out.println(standardSubmit);
        return true;
    }
}
